import 'package:bilibili/http/core/sun_error.dart';
import 'package:bilibili/http/dao/login_dao.dart';
import 'package:bilibili/navigator/sun_navigator.dart';
import 'package:bilibili/util/string_util.dart';
import 'package:bilibili/util/toast_util.dart';
import 'package:bilibili/widget/appbar.dart';
import 'package:bilibili/widget/login_button.dart';
import 'package:bilibili/widget/login_effect.dart';
import 'package:bilibili/widget/login_input.dart';
import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  static int PASSWORD_LOGIN = 1;
  static int CODE_LOGIN = 2;
  var loginWay = PASSWORD_LOGIN;
  bool protect = false;
  bool loginEnable = false;
  String? username;
  String? password;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(loginWay == PASSWORD_LOGIN ? "密码登录" : "短信登录",
          loginWay == PASSWORD_LOGIN ? "短信登录" : "密码登录", () {
        setState(() {
          loginWay == PASSWORD_LOGIN
              ? loginWay = CODE_LOGIN
              : loginWay = PASSWORD_LOGIN;
        });
      }),
      body: Container(
        child: ListView(
          children: [
            LoginEffect(protect: protect),
            LoginInput(
              "账号",
              "请输入手机号或邮箱",
              onChanged: (text) {
                username = text;
                checkInput();
              },
            ),
            LoginInput(
              "密码",
              "请输入密码",
              rightText: "忘记密码？",
              obscureText: true,
              onChanged: (text) {
                password = text;
                checkInput();
              },
              focusChanged: (focus) {
                setState(() {
                  protect = focus;
                });
              },
            ),
            Padding(
              padding: EdgeInsets.only(left: 15, right: 15, top: 30),
              child: Row(
                children: [
                  Visibility(
                      child: Expanded(
                        child: LoginButton(
                          "注册",
                          isOutLine: true,
                          onPressed: () {
                            setState(() {
                              loginWay = CODE_LOGIN;
                            });
                          },
                        ),
                      ),
                      visible: loginWay == PASSWORD_LOGIN),
                  SizedBox(
                    width: 15,
                  ),
                  Expanded(
                    child: LoginButton(
                      loginWay == PASSWORD_LOGIN ? "登录" : "验证登录",
                      enable: loginEnable,
                      onPressed: login,
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  checkInput() {
    bool enable = isNotEmpty(username) && isNotEmpty(password);
    setState(() {
      loginEnable = enable;
    });
  }

  void login() async {
    try {
      var result = await LoginDao.login(username!, password!);
      if (result["code"] == 0) {
        showToast("登录成功");
        SunNavigator.getInstance().onJumpTo(RouteStatus.home);
      } else {
        showWarnToast(result["msg"]);
      }
    } on NeedAuth catch (e) {
      print(e);
      showWarnToast(e.message);
    } on SunNetError catch (e) {
      showWarnToast(e.message);
    }
  }
}
